if select(2, UnitClass("player")) ~= "SHAMAN" then return end
local beSch = CreateFrame("Frame", nil, UIParent)
local playerName, playerRealm = UnitName("player")

beSch:RegisterEvent"PLAYER_LOGIN"
beSch:RegisterEvent"COMBAT_LOG_EVENT_UNFILTERED"

-- Settings start
        
    --position, sizing and anchor
        local settingPositionX = 0 --horiz
        local settingPositionY = -150 --vert
        local settingFrameAnchor = "CENTER"
        
        -- these are the sizes of the frame
        -- the name text is anchored on top center
        -- the numbers on left and right bottom
        -- so if you change the sizes the distances will change
        local settingFrameHeight = 40
        local settingFrameWidth = 40
    
    --font and color
        local settingFont = "Fonts\\ARIALN.ttf"
        local settingFontSize = 20
        local settingFontSizeSecond = 10
        local settingColorZero = { r=255/255, g=0/255, b=0/255 }
        local settingColorWater = RAID_CLASS_COLORS[select(2, UnitClass("player"))]
        local settingColorEarth = { r=165/255, g=255/255, b=69/255 }
        local settingColorNormal = { r=1, g=1, b=1 }
        local settingShadow = true
        local settingOutline = false
        local settingAlpha = 1
    
    --notifications (text,audio)
        -- text
        local settingNotifyTextES = true
        local settingNotifyTextWS = false
        -- audio
        local settingNotifyAudioEs = true
        local settingNotifyAudioEsURL = "Sound\\Event Sounds\\Event_wardrum_ogre.wav"
        local settingNotifyAudioWs = true
        local settingNotifyAudioWsURL = "Sound\\Doodad\\BellTollAlliance.wav"
        
-- Settings end

local beSchVarWaterShieldName = select(1,GetSpellInfo(33736))
local beSchVarWaterShieldCount = 0
local beSchVarWaterShieldActive = false

local beSchVarEarthShieldName = select(1,GetSpellInfo(32594))
local beSchVarEarthShieldTarget = ""
local beSchVarEarthShieldCount = 0
local beSchVarEarthShieldActive = false
local beSchVarEarthShieldNotMine = false

function beSch:ChatMessage(message) -- write normal messages
    -- default ChatFrame1
    ChatFrame1:AddMessage("|cff0033CCbe|r|cff888888Sch|r: " .. message); 
end

function beSch:DebugMessage(message) -- write normal messages
    -- default ChatFrame1
    ChatFrame4:AddMessage("|cff0033CCbe|r|cff888888Sch|r: " .. message); 
end

function beSch:Initialize()
    textES = self:CreateFontString(nil, "OVERLAY")
   	textES:SetPoint("LEFT", self)
	textES:SetTextColor(settingColorEarth.r, settingColorEarth.g, settingColorEarth.b)
	
    textWS = self:CreateFontString(nil, "OVERLAY")
   	textWS:SetPoint("RIGHT", self)
	textWS:SetTextColor(settingColorWater.r, settingColorWater.g, settingColorWater.b)
	
	textSecond = self:CreateFontString(nil, "OVERLAY")
   	textSecond:SetPoint("TOP", self)
	textSecond:SetTextColor(settingColorNormal.r, settingColorNormal.g, settingColorNormal.b)
	
    if settingOutline == true then
		textES:SetFont(settingFont, settingFontSize, "OUTLINE")
		textWS:SetFont(settingFont, settingFontSize, "OUTLINE")
		textSecond:SetFont(settingFont, settingFontSizeSecond, "OUTLINE")
	else
		textES:SetFont(settingFont, settingFontSize, nil)
		textWS:SetFont(settingFont, settingFontSize, nil)
		textSecond:SetFont(settingFont, settingFontSizeSecond, nil)
	end
	
	if settingShadow == true then
		textES:SetShadowOffset(1,-1)
		textWS:SetShadowOffset(1,-1)
		textSecond:SetShadowOffset(1,-1)
	end
	
  	self:SetPoint(settingFrameAnchor, UIParent, settingFrameAnchor, settingPositionX, settingPositionY)
	self:SetHeight(settingFrameHeight)
	self:SetWidth(settingFrameWidth)
	self:SetAlpha(settingAlpha)
end

function beSch:FrameUpdate() --write to frame
    if beSchVarEarthShieldNotMine then
        textSecond:SetTextColor(settingColorZero.r, settingColorZero.g, settingColorZero.b)
    else
        textSecond:SetTextColor(settingColorNormal.r, settingColorNormal.g, settingColorNormal.b)
    end
    
    if beSchVarEarthShieldCount ~= 0 then
        textES:SetTextColor(settingColorEarth.r, settingColorEarth.g, settingColorEarth.b)
    else
        textES:SetTextColor(settingColorZero.r, settingColorZero.g, settingColorZero.b)
    end
    
    if beSchVarWaterShieldCount ~= 0 then
        textWS:SetTextColor(settingColorWater.r, settingColorWater.g, settingColorWater.b)
    else
        textWS:SetTextColor(settingColorZero.r, settingColorZero.g, settingColorZero.b)
    end
    
    textES:SetText(beSchVarEarthShieldCount)
    textWS:SetText(beSchVarWaterShieldCount)
    textSecond:SetText(beSchVarEarthShieldTarget)
end

-- cl event
beSch:SetScript("OnEvent", function(self, event, timeStamp, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags) 
    if event=="PLAYER_LOGIN"  then
        beSch:Initialize()    
    elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then       
        if eventType =="SPELL_CAST_SUCCESS" then 
            local castName = arg10
            if sourceName == playerName then
                if castName == beSchVarEarthShieldName then
                    beSchVarEarthShieldNotMine = false
                    beSchVarEarthShieldActive = true
                    beSchVarEarthShieldTarget = destName
                    beSchVarEarthShieldCount = 6
                elseif castName == beSchVarWaterShieldName then
                    beSchVarWaterShieldActive = true
                    beSchVarWaterShieldCount = 3
                end
            else
                if destName == beSchVarEarthShieldTarget then
                    if castName == beSchVarEarthShieldName then
                        beSchVarEarthShieldNotMine = true
                        beSchVarEarthShieldActive = true
                        beSchVarEarthShieldCount = 6
                    end
                end
            end
        elseif eventType =="SPELL_AURA_REMOVED" then 
            -- remove
            local castName = arg10
            if castName == beSchVarEarthShieldName then
                if beSchVarEarthShieldActive and destName == beSchVarEarthShieldTarget then           
                    beSchVarEarthShieldActive = false
                    beSchVarEarthShieldCount = 0
                    if settingNotifyTextES then                
        		        beSch:ChatMessage(beSchVarEarthShieldTarget .." Earthshield faded")
                    end  
                    if settingNotifyAudioEs then
        			    PlaySoundFile(settingNotifyAudioEsURL)
        		    end            
                end
            elseif castName == beSchVarWaterShieldName then
                if beSchVarWaterShieldActive and destName == playerName then                        
                    beSchVarWaterShieldActive = false
                    beSchVarWaterShieldCount = 0    
                    if settingNotifyTextWS then                
        		        beSch:ChatMessage("WaterShield faded")
                    end  
                    if settingNotifyAudioWs then
        			    PlaySoundFile(settingNotifyAudioWsURL)
        		    end
    		    end
            end
        elseif eventType =="SPELL_HEAL" then 
            -- earthshield proc  
            local castName = arg10 
            if beSchVarEarthShieldActive and destName == beSchVarEarthShieldTarget and castName == beSchVarEarthShieldName then
                if beSchVarEarthShieldCount > 0 then -- if more then -1
                    beSchVarEarthShieldCount = beSchVarEarthShieldCount - 1
                end
            end
        elseif eventType =="SPELL_ENERGIZE" then 
            -- watersield proc 
            local castName = arg10 
            if beSchVarWaterShieldActive and destName == playerName and castName == beSchVarWaterShieldName then
                if beSchVarWaterShieldCount > 0 then
                    beSchVarWaterShieldCount = beSchVarWaterShieldCount -1
                end
            end
        elseif eventType =="UNIT_DIED" then
            if beSchVarWaterShieldActive and destName == playerName then
                beSchVarWaterShieldActive = false
                beSchVarWaterShieldCount = 0      
            end
            if beSchVarEarthShieldActive and destName == beSchVarEarthShieldTarget then
                beSchVarEarthShieldActive = false
                beSchVarEarthShieldCount = 0
            end
        end
     end 
beSch:FrameUpdate() 
end)
